function init(){
	var page = 1;
	var rows = 10;
	$(".select-box ul").on('click','li',function(){
	    $(this).parent().siblings('.show').val($(this).html());
	    $(this).parent().siblings('.hidden').val($(this).attr('data-id'));
	});
	$(".select-box div.fl").click(function(e){
	    e.stopPropagation();
	    var div=$(this);
	    div.children('ul').toggleClass('hide');
	    $(document).click(function() {
	        if (!div.children('ul').hasClass("hide")) {
	            div.children('ul').addClass("hide");
	        }
	    });
	});
	$('.top-block ul li').click(function(){
	    $(this).addClass('active').siblings('li').removeClass('active');
	});
	// 搜索框搜索
    $('.search-box').on('click','span',function(){
    	var conditions = findSearchConditions();
    	loadComment(page,rows,conditions[0],conditions[1],conditions[2],conditions[3]);
    });
    // 仓库名称搜索
    $('#warehouseSearchBox').on('click','li',function(){
    	var conditions = findSearchConditions();
    	loadComment(page,rows,conditions[0],conditions[1],conditions[2],conditions[3]);
    })
    // 商品分类搜索
    $('#productTypeSearchBox').on('click','li',function(){
    	var conditions = findSearchConditions();
    	loadComment(page,rows,conditions[0],conditions[1],conditions[2],conditions[3]);
    })
	// 库存状态搜索
    $('#isRelease').on('click','li',function(){
    	var conditions = findSearchConditions();
    	loadComment(page,rows,conditions[0],conditions[1],conditions[2],conditions[3]);
    })
	loadSearchBox();
	loadComment(page,rows,'','','','');
	//商品分类
	//商品分类下拉框
    $('.categoryList').on('click','s',function(e){
    	e.stopPropagation();
    	$(this).toggleClass('on');
    	$(this).siblings('ul').css('display',$(this).siblings('ul').css('display') == 'none'?'block':'none');
    });
    $(".product-type div.fl").click(function(e){
        e.stopPropagation();
        var div=$(this);
        div.children('ul').toggleClass('hide');
    });
    $('.categoryList').on('click','b',function(){
		$('.product-type').find('.show').val($(this).html());
		$('.product-type').find('.hidden').val($(this).parent('li').attr('data-id'));
		var conditions = findSearchConditions();
    	loadComment(page,rows,conditions[0],conditions[1],conditions[2],conditions[3]);
    });
}
/**
 * 获取所有的查询条件
 */
function findSearchConditions(){
	var strs = new Array();
	strs[0] = $('#productName').val();
	strs[1] = $('#warehouseId').val();
	strs[2] = $('#categoryId').val();
	var isRelease;
	var isReleases = $('#isRelease').children();
	isReleases.each(function(){
	    if($(this).hasClass('active')){
	    	isRelease = $(this).attr('data-id');
	    	return false;
	    }
	});
	strs[3] = isRelease;
	return strs;
}

/**
 * 出入商品详情
 */
function detail(id){
	window.location.href = "/DistributorSystem/warehouse/findOutInProductInfoById?id="+id;
}

/**
 * 加载选择搜索框数据
 * @param page
 * @param rows
 */
function loadSearchBox() {
	/*
	 * 仓库选择框赋值
	 */
	$.ajax({
		type: "post",
		url: '/diconet/warehouse/warehouse/list/all',
		dataType: 'json',
		success: function(data){
			var commentContent = '<li>请选择</li>';
			if(data) {
				for(var i=0; i<data.length; i++) {
					var comment = data[i];
					var date1 = comment.publishDate == null ? '' : comment.publishDate;
					commentContent += '<li data-id="'+comment.id+'">' + comment.warehouseName+'</li>';
				}
				$('#warehouseSearchBox').html(commentContent);
			}
		},
		error : function(data){
			layer.msg('仓库搜索条件加载失败', {time:500,icon:5});
		}
	});
	/*
	 * 商品类型选择框赋值
	 */
	$.ajax({
        type:'post',
        url:'/diconet/productCategory/findAllProdCategory',
        success:function(data){
        	var list = data.data;
        	var level1 = "";
        	for(var i=0;i<list.length;i++){
        		if(list[i].parentId == ""){
        			var isParent = 0;
        			for(var j=0;j<list.length;j++){
//        				判断该分类是否含有子分类
        				if(list[i].id == list[j].parentId){
        					isParent = 1;
        				}
        			}
        			if(isParent == 0){
        				level1 += "<li class='last-type' data-id='"+list[i].id+","+list[i].level+"'><b>"+list[i].categoryName+"</b><ul></ul></li>";
        			}else if(isParent == 1){
        				level1 += "<li data-id='"+list[i].id+","+list[i].level+"'><s>▼</s><b>"+list[i].categoryName+"</b><ul></ul></li>";
        			}
        		}
        	}
        	$('.categoryList').html(level1);
        	$('.categoryList>li').each(function(){
        		var level2 = "";
        		var id = $(this).attr('data-id').split(',')[0];
        		for(var i=0;i<list.length;i++){
            		if(list[i].parentId == id){
            			var isParent = 0;
            			for(var j=0;j<list.length;j++){
//            				判断该分类是否含有子分类
            				if(list[i].id == list[j].parentId){
            					isParent = 1;
            				}
            			}
            			if(isParent == 0){
            				level2 += "<li class='last-type' data-id='"+list[i].id+","+list[i].level+"'><b>"+list[i].categoryName+"</b><ul></ul></li>";
            			}else if(isParent == 1){
            				level2 += "<li data-id='"+list[i].id+","+list[i].level+"'><s>▼</s><b>"+list[i].categoryName+"</b><ul></ul></li>";
            			}
            		}
            	};
            	$(this).children('ul').html(level2);
        	});
        	$('.categoryList>li>ul>li').each(function(){
        		var level3 = "";
        		var id = $(this).attr('data-id').split(',')[0];
        		for(var i=0;i<list.length;i++){
            		if(list[i].parentId == id){
            			level3 += "<li data-id='"+list[i].id+","+list[i].level+"' class='last-type'><b>"+list[i].categoryName+"</b><ul></ul></li>";
            		}
            	};
            	$(this).children('ul').html(level3);
        	})
        },
        error:function(){}
    });
}

/**
 * 加载列表数据
 * @param page
 * @param rows
 */
function loadComment(page, rows, productName, warehouseId, categoryId, isRelease) {
	$.ajax({
		type: "post",
		url: '/diconet/warehouse/warehouseProduct/findWarehousProduct',
		data: {
			page : page,
			rows : rows,
			productName : productName,
			warehouseId : warehouseId,
			categoryId : categoryId,
			isRelease : isRelease,
		},
		dataType: 'json',
		success: function(data){
			var commentContent = '';
			if(data) {
				for(var i=0; i<data.rows.length; i++) {
					var comment = data.rows[i];
					var date1=comment.publishDate == null?'':comment.publishDate;
					commentContent += '<tr>';
					commentContent += '<td><div><img src="/docs/'+comment.productImgUrl+'" alt=""/></div></td>';// 商品图片
					commentContent += '<td><p>'+comment.productCode+'</p><p>'+comment.productName+'</p></td>';// 商品代码商品名称
					var htmlEle = "";
					var specValues = comment.productPriceId;
					var specValue = specValues.split(",");
					for(var num=0; num<specValue.length; num++){
						htmlEle += "<p>"+specValue[num]+"</p>"
					}
					commentContent += '<td>'+htmlEle+'</td>';// 规格
					commentContent += '<td>'+comment.categoryName+'</td>';// 分类
					commentContent += '<td>'+comment.brandName+'</td>';// 品牌
					commentContent += '<td>'+comment.warehouseName+'</td>';// 仓库
					commentContent += '<td>'+comment.inventoryQuantity+'</td>';// 所在仓库库存
					commentContent += '<td>'+comment.bigUnitName+comment.littleUnitName+'</td>';// 单位
					commentContent += '<td><button class="detail" onclick="detail(\''+comment.id+'\')">详情</button></td>';// <button class="out-in">出/入库</button>
					commentContent += '</tr>';
				}
				$('.product-list-tbody').html(commentContent);
				var dataPage = data.page;
				var pageContent="";
				if(dataPage.firstPage) {
					pageContent += '<span class="disabled">首页</span>';
					pageContent += '<span class="disabled">上一页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.first+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">首页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.prev+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">上一页</a>';
				}
				var maxPage = dataPage.page+2 < dataPage.last?dataPage.page+2:dataPage.last;
				if(dataPage.page >= 4 && dataPage.page + 2 < dataPage.last){
					for(var i=dataPage.page-2; i<= maxPage; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.page > 4 && dataPage.page + 2 >= dataPage.last){
					for(var i=dataPage.last-4; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else if(dataPage.last < 5){
					for(var i=dataPage.first; i<=dataPage.last; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}else{
					for(var i=dataPage.first; i<=5; i++) {
						if(i == dataPage.page) {
							pageContent += '<span class="current">'+i+'</span>';
						} else {
							pageContent += '<a href="javascript:loadComment('+i+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">'+i+'</a>';
						}
					}
				}
				if(dataPage.lastPage) {
					pageContent += '<span class="disabled">下一页</span>';
					pageContent += '<span class="disabled">尾页</span>';
				} else {
					pageContent += '<a href="javascript:loadComment('+dataPage.next+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">下一页</a>';
					pageContent += '<a href="javascript:loadComment('+dataPage.last+','+rows+',\''+productName+'\',\''+ warehouseId+'\',\''+ categoryId+'\',\''+isRelease+'\');">尾页</a>';
				}
			}
			$(".quotes").html(pageContent);
		},
		error : function(data){
			layer.msg('请求失败', {time:500,icon:5}); 
		}
	});
}
init();
